<?php

namespace app\models;

use Yii;
use yii\base\Model;

/**
 * ContactForm is the model behind the contact form.
 */
class ContactForm extends Model
{
    public $name;
    public $email;
    public $body;
    public $verifyCode;


    /**
     * @return array the validation rules.
     */
    public function rules()
    {
        return [
            // name, email, subject and body are required
            [['name', 'email', 'body'], 'required'],
            // email has to be a valid email address
            ['email', 'email'],
            // verifyCode needs to be entered correctly
            ['verifyCode', 'captcha'],
        ];
    }

    /**
     * @return array customized attribute labels
     */
    public function attributeLabels()
    {
        return [
            'name' => '称呼',
            'email' => '电子邮箱',
            'body' => '留言内容',
            'verifyCode' => '验证码',
        ];
    }

    /**
     * Sends an email to the specified email address using the information collected by this model.
     * @return bool whether the model passes validation
     */
    public function contact()
    {
        if ($this->validate()) {
            $bodyHtml = '<div class="template" style="width: 750px; border: solid 1px #e5e5e5;"> <div class="header" style="padding: 15px 25px 10px 30px; color: #6b6b6b;font-size: 20px; line-height: 1; border-bottom: solid 1px #d4d4d4; background: #59A8E7;"> <span style="font-weight: 700; color: #fff;font-size: 22px;">XY Team</span> <br> <p style="font-size: 12px; color: #fff;">专业开发团队，提供网站、微信、APP、H5等解决方案。</p> </div>';
            $bodyHtml.= '<div class="content" style="font-size:14px;padding: 15px 25px 15px 30px; color: #6b6b6b;"> <b>'.Yii::$app->name.'</b>,<br> <br> 您好！您有新的客户咨询信息。 <br>';
            $bodyHtml.= '<p style="margin-left:30px;">';
            $bodyHtml.= '称　　呼：'. $this->name.'<br>';
            $bodyHtml.= '电子邮箱：'. $this->email.'<br>';
            $bodyHtml.= '留言内容：'. $this->body.'<br>';
            $bodyHtml.= '<hr><br> <b>www.xinyeweb.com</b><br> </div>';
            $bodyHtml.= '<div class="footer" style="padding: 15px 25px 15px 25px; color: #6b6b6b;background: #eeeeee;overflow:auto; font-size: 12px; border-top: solid 1px #e6e6e6"> <div style="display: inline-block; line-height: 1.5; width:250px; float: left"> <b style="border-bottom: solid 1px #afafaf"></b>中国大陆<br> (86) <span style="border-bottom:1px dashed #ccc;z-index:1" t="7" onclick="return false;" data="17707561655">17707561655</span></div> <div style=" display: inline-block; ; ; ; "> <b style="border-bottom: s=olid 1px #afafaf">  </b><br></div> </div></div>';

            Yii::$app->mailer->compose()
                ->setTo("232767044@qq.com")
                ->setFrom(['sales@xinyeweb.com' => Yii::$app->name])
                ->setSubject("站点留言")
                ->setTextBody($this->body)
                ->setHtmlBody($bodyHtml)
                ->send();

            return [
                'state' => 1,
                'msg' => '发送成功'
            ];
        } else {
            $msg = '';
            foreach ($this->getFirstErrors() as $v) {
                $msg .= $v;
            }
            return [
                'state' => 0,
                'msg' => $msg
            ];
        }

    }

}
